package cqrtplm.aop;

import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.doc.vo.DocDetails;
import com.hustcad.plm.pdm.iba.service.TyplmAttributeValueService;
import com.hustcad.plm.pdm.iteration.dto.TyplmCommonHistoryDTO;
import com.hustcad.plm.pdm.partbom.vo.QueryDescribeObjVO;
import com.ty.basic.constant.TableTypeConstrant;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.vo.AttributeVO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;


@RefreshScope
@Aspect
@Component
public class DocVersionHistoryAOP {

    @Resource
    private TyplmAttributeValueService typlmAttributeValueService;

    @AfterReturning(value = "execution(* com.hustcad.plm.pdm.iteration.service.impl.TyCommonHistoryServiceImpl.getVersionHistory(..))", returning = "keys")
    public void doAfterReturning(JoinPoint joinPoint, Object keys) {
        Object[] args = joinPoint.getArgs();//获取入参
        IdentifierEntity identifierEntity = (IdentifierEntity) args[0];
        if(!TableTypeConstrant.TY_DOC_OTYPE.equals(identifierEntity.getOtype()) && !TableTypeConstrant.TY_CADDOC_OTYPE.equals(identifierEntity.getOtype())){
            return;
        }
        List<TyplmCommonHistoryDTO> list = (List<TyplmCommonHistoryDTO>) keys;
        List<BigInteger> oIds = list.stream().map(TyplmCommonHistoryDTO::getOid).collect(Collectors.toList());
        Map<String, List<AttributeVO>> ibaAttrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                oIds, identifierEntity.getOtype(), Arrays.asList("ChangeDescription","ChangeDocumentNumber","BusinessEdition"));
        list.stream().filter(Objects::nonNull).filter(historyDTO -> historyDTO.getOid() != null)
                .forEach(historyDTO -> {
                    List<AttributeVO> attributeVOS = ibaAttrValueByOidAndKeys.get(historyDTO.getOid().toString());
                    historyDTO.setIbaAttrs(attributeVOS);
                });
    }


    @AfterReturning(value = "execution(* com.hustcad.plm.pdm.partbom.service.impl.TyplmPartDescribeLinkImpl.queryPartDescribeDoc(..))", returning = "keys")
    public void queryPartDescribeDoc(JoinPoint joinPoint, Object keys) {
        List<QueryDescribeObjVO> list = (List<QueryDescribeObjVO> ) keys;
        List<BigInteger> oIds = new ArrayList<>();
        for(QueryDescribeObjVO queryDescribeObjVO : list){
            List<ResultEntityVO> describeObjList = queryDescribeObjVO.getDescribeObjList();
            for(ResultEntityVO resultEntityVO : describeObjList){
                oIds.add(resultEntityVO.getOid());
            }
        }
        Map<String, List<AttributeVO>> ibaAttrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                oIds, TableTypeConstrant.TY_DOC_OTYPE, Collections.singletonList("BusinessEdition"));
        for(QueryDescribeObjVO queryDescribeObjVO : list){
            List<ResultEntityVO> describeObjList = queryDescribeObjVO.getDescribeObjList();
            for(ResultEntityVO resultEntityVO : describeObjList){
                if(ibaAttrValueByOidAndKeys.containsKey(resultEntityVO.getOid().toString())) {
                    List<AttributeVO> attributeVOS = ibaAttrValueByOidAndKeys.get(resultEntityVO.getOid().toString());
                    resultEntityVO.setIbaAttrs(attributeVOS);
                }
            }
        }
    }


    @AfterReturning(value = "execution(* com.hustcad.plm.pdm.middle.partbom.service.impl.TyplmPdmPartServiceImpl.getPartLinkReferenceDoc(..))", returning = "keys")
    public void getPartLinkReferenceDoc(JoinPoint joinPoint, Object keys) {
        List<DocDetails> list = (List<DocDetails> ) keys;
        List<BigInteger> oIds = new ArrayList<>();
        String otype = "";
        for(DocDetails docDetails : list){
            oIds.add(new BigInteger(docDetails.getOid()));
            otype = docDetails.getOtype();
        }
        Map<String, List<AttributeVO>> ibaAttrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                oIds, otype, Collections.singletonList("BusinessEdition"));

        bulidIbaMap(list, ibaAttrValueByOidAndKeys);

    }

    @AfterReturning(value = "execution(* com.hustcad.plm.pdm.partbom.service.impl.TyplmPartRelObjectServiceImpl.queryPartRelatedDocInfo(..))", returning = "keys")
    public void queryPartRelatedDocInfo(JoinPoint joinPoint, Object keys) {
        List<DocDetails> list = (List<DocDetails> ) keys;
        List<BigInteger> oIds = new ArrayList<>();
        String otype = "";
        for(DocDetails docDetails : list){
            oIds.add(new BigInteger(docDetails.getOid()));
            otype = docDetails.getOtype();
        }
        Map<String, List<AttributeVO>> ibaAttrValueByOidAndKeys = typlmAttributeValueService.getIbaAttrValueByOidAndKeys(
                oIds, otype, Collections.singletonList("BusinessEdition"));
        bulidIbaMap(list, ibaAttrValueByOidAndKeys);
    }

    private void bulidIbaMap(List<DocDetails> list, Map<String, List<AttributeVO>> ibaAttrValueByOidAndKeys) {
        for(DocDetails docDetails : list) {
            Map<String, List<String>> map = new HashMap<>();
            if(ibaAttrValueByOidAndKeys.containsKey(docDetails.getOid())){
                List<AttributeVO> attributeVOS = ibaAttrValueByOidAndKeys.get(docDetails.getOid());
                for (AttributeVO attributeVO : attributeVOS) {
                    map.put(attributeVO.getAttributeEnName(), Collections.singletonList(attributeVO.getAttributeValue()));
                }
                docDetails.setIbaAttributeMap(map);
            }

        }
    }


}
